(navigation image)
Home American Libraries | Canadian Libraries | Universal Library | Community Texts | Project Gutenberg | Children's Library | Biodiversity Heritage Library | Additional Collections
Search: Advanced Search
Anonymous User (login or join us)
Upload
See other formats

Full text of "Arduino"

Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



.1 



Make Projects 



build, hack, tweak, share, discover. 



Connect an iPhone, iPad, or 

iPod touch to Arduino with the 

Redpark Serial Cable 

Written By: Brian Jepson 



PARTS: 

Breakout Pack for Arduino and iPhone (1) 
iPhone. iPad or iPod touch (1) 
Arduino Uno (1) 



SUMMARY 

The Redpark Serial Cable, which is part of the Redpark Breakout Pack for Arduino and iOS 
available from the Maker Shed , lets you connect the iPhone to Arduino without jail breaking. 
This guide shows you one of the simplest examples: connecting an iOS device to an Arduino 
and turning an LED on and off from an iOS app. 

(This guide written with lots of help from Alasdair Allan ), the author of a book on this very 
topic, iOS Sensor Apps with Arduino . 



© Make Projects www.makeprojects.com Page 1 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 1 — Wire up the RS232 adapter to the Arduino 




• Connect the RS232 adapter's 
power and ground to the Arduino's 
5V and GND pins, respectively. 

• Connect the RS232 adapter's TX to 
the Arduino's RX, and the RS232 
adapter's RX to the Arduino's TX. 



Step 2 — Make sure you're set up for iOS development 



V Developer 



Technologies Resource* P-uydf-rs Support Member Cen 



iOS Developer Program 

The fastest path from code to customer. 






1. Develop 



Develop vojr aopliciiioit wh the iQSSDK 
and a wealth of ledrniCa! n 
iLUDev Center. Lean 



Test and debug yaurcoto on iPad, 
Phone and Pt:.j BbltOl to finalize your 
applications. Lear n more ► 



|^J 



3. Distribute 

Disrriouie yourappsoi The App Score 
and reach millions Of iPad, iPhone. and 
iPod tooth users, i earn more ► 



• To develop apps for the iPhone, 
iPad, or iPod touch, you'll need to 
register as an iOS developer. 
Although you can register for free, 
all that gets you is the development 
tools. You won't be able to deploy 
your apps to a real device. It's $99 
a year for an individual developer, 
$299 for Enterprises (this option 
lets you deploy apps directly to 
your team), and free for 
educational institutions (which also 
lets you deploy apps to a team). 

• For more information, see 
the iOS Developer Program . 



© Make Projects 



www.makeprojects.com 



Page 2 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 3 — Make sure you can run an app on your iOS device 











#»o n 






"--'' ; ' iPad 4.3 


Simulator ^ 
.3 Simulator 




HelloArd Edlt Scheme... 
— J 2 targets. NewScheme... .dUr 
»' Extern Manage Schemes... 




t M - 


► □lib 

" IJHdlQArdumo 


// Release ar 
// e.g. self 









• If you've never programmed for the 
iPhone before, you can't count on 
this guide to help you. There are 
many learning resources, from 
Stanford's free Developing Apps for 
iOS courses to Matt Neuburg's 
Programming iOS 5 or Alasdair 
Allan's Learning iOS Programming , 
both of which are available in print 
or ebook form. 

• Before you go any further, please 
make sure you can create and run 
simple apps on your iPhone, iPad, 
or iPod touch. It will also be helpful 
if you familiarize yourself 
thoroughly with the Xcode 
development environment. 



© Make Projects 



www.makeprojects.com 



Page 3 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 4 — Create a new view-based project 




K 



l iir.,: .,■ ,,|:'i,vi, fftl ymr n«W prOjt* 1 






Product fc.amc 


\f r.U.-.rt,,--, 






Company ld^ntifi«r 
1 Bundle Identifier 


5pm,m*l(Cprgj«II 


r 


Wf Include Ufl [T*i« 



' Cintel ) 



f Previous ; ( Ue« ) 



• In Xcode, create a new view-based project. 

• Give you project the name HelloArduino, and choose the appropriate device family 
(iPhone, iPad, or if you're feeling ambitious, Universal, though this will create a bit more 
work for you). 



© Make Projects 



www.makeprojects.com 



Page 4 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 5 — Add a switch to your app 



HelloAiduiiw 

— 2 targets, iOS5DK4.1 

j HelloArduino 

h Helloircyin&AacDelcqate 1 ! 
n HelloArduinoAppDekgate.m 

Main Wind Dwxitj 
h ^elloA/duJroVi-ewConlrolFer.li 
n: HdloArduirioViewCorrtroikr.m 

f ;__, Supporting Files 
■ ■[_ HelloArduinoTestJ 
• | ~~i Frameworks 
Prgdu-cls 



(►) (S) \ HdlcA.. 



HelloArduino- HelloArduiPoViewControiller.itib 



sl'sakco -i[5 



HLMJdJOEni LMJ 

Editor View Organizer 



Ti 3 A ™ 



_jB?l!aArdUiino - 



H H 



■View ■ ;Swiwri 



IJ 





Vertical 

QwteatQ Highlighted 

Q Selected fl Enabled 




Background I 



□ {) i • i 



si?ivi n g the boolean itate or i ■■-; 

Al'3~:-i3p.: ig fit con:rci ;: 



IffT Switch - Displays di el4*neni 



Ji 



Activity Indicator View - Provides 

feetJbatk on the progress ol a task or 
process o J jntnown deration. 



Progress View - Depicts the 
P"flgi«* «f a task (*«• lime. 



• On the left pane of the screen, 
expand your project, and open the 
folder HelloArduino. 

• Next, locate the 
HelloArduinoViewController.xib and 
click on it. This will bring up your 
app's main view, which is blank at 
the moment. 

• Bring up the Object Library 
(View->Utilities^Object Library), 
and drag a Switch to the center of 
your view. 

• Next, show the Attributes Inspector 
(View^Utilities->Attributes 
Inspector) and set the switch's 
State to Off. 



© Make Projects 



www.makeprojects.com 



Page 5 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 6 — Wire up the switch to your code 




^Bi SiS^ IT^^^^E^^w^^fc^^ ^^^^^r^ 









• Now you need to tell the app's code what to do with the switch. 

• If you need room on the screen, you can hide the Utilities Pane 
(View->Utilities^>Hide Utilities). 

• Next, open the Assistant Editor (View->Editor^Assistant), and locate the toolbar above 
the window that appears. 

• Click the icon to the right of the left/right arrows and make sure the Assistant Editor is set 
to automatic. It should be showing the HelloArduinoViewController.h file. 

• Control-click the switch, and drag from it to the code that appears in the Assistant Editor. 
Hold it just above the "@end" in the code and release. Add a new outlet and name it 
toggleSwitch as shown. 

• Do this once again, but instead, add a new action, and name it toggleLED. Save 
the file (File^Save). 



* 



© Make Projects 



www.makeprojects.com 



Page 6 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 7 — Import the Redpark Serial Cable library 






Choose options For adding Lhese Files: 




1 


Destination PfCopy items into destination group's folder (if needed) 

Folders © Create groups for any Added folders 

O&eate folder references for any added folders 






Add t& targets A HelloAfdJuirw 

3 rtallftArduirtoTisH 
















( Cancel ) fr- Hnlah ' 





• The Redpark Serial Cable installation will create a folder in your home directory 
named Redpark Serial SDK. 

• You can download the SDK from the Redpark web site . 






• Open this in the Finder, and select the inc and lib folders, then drag them to your project 
as shown. 

• A dialog will appear; make sure you check the option "Copy items into destinations group's 
folder (if needed)" and click Finish. 



© Make Projects 



www.makeprojects.com 



Page 7 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 8 — Import the iOS Accessory Framework 




«no 



i HelloArduino - HelloArduino.X' 



Run 



Stop 



I n I n ® A = » m 



( HelloArduino 



*Jff'W..i-..iH 



» CUBelloArduino 

[hj HelloArduinoAppDelegate.fi 
(ml HelloArduinoAppDeiegate.m 
■■■' MainWindow.xib 
g HellQArduinoViewController.ri 
£ HelloArduinoViewControlkr.m 
■ HelloArduinoViewConi 
► [J Supporting Files 
► PlHelloArduinoTests 
I ~j Framework* 
Products 



b 



PROJ ECT 

HelloArdu.. 



Jk. 



'"'; HelloArdu 



Build Settings Build Phases 



Target Dependencies (0 items) 



Compile Sources {3 item?) 



Link Binary With Libraries {4 items) 



& UlKit.framework 
|** Foundation. framework 
!*■ CoreGraphics. framework 
libRscMgrUniv.a 



rder Irameworks 




• Next, you need to import Apple's 
Accessory Framework. To do this, 
click the Project in the left pane, 
then choose Build Phases, and 
open the "Link Binary With" 
section. Click +, locate the 
External Accessory. framework, and 
add it. 



Step 9 — Add some declarations to the view controller 



! | 4 ► | L^HelloArduir 



HelloArdu 



h HelloArduinoViewController. h No Selectia 



T7 

// HelloArduinoViewCon trailer, h 

// rlelloArduino 

U 

U Created by Brian Jepson on 7/16/11. 

// Copyright 2B11 O'Reilly Media. All rights reserved. 

it 

tfinport <UIKit/UIKit.h> 
#ifnport "RscMgr.h" 

#define BUFFER_LEN 1024 

^interface HelloArduinoViewController : UTViewCont roller -eRscMgrDelegatei* { 

RscMgr *rscMgr: 

UlntB rxBuffer[BUFFER_LEN] ; 

UlntB txBuffer[BUFFER_LEN] ; 

UlSwitch *toggleEwitch; 
> 

^property (nonatomic, retain} IBDutlet UlSwitch +toagle5witch: 
- (IBAction)toggleLEB: (ic) sender; 



• Select the 
HelloArduinoViewController. h file to 
open it for editing. 

• Visit my modified version of the file 
at GitHub . 

• Edit your copy of the file: 

• Add the two lines between 

#import <UIKit/UIKit.h> 
and @interface 

• Modify the @interf ace line to 
match mine. 

• Add the three lines just above 

UlSwitch *toggleSwitch; . 

• Save the file (File^Save). 



© Make Projects 



www.makeprojects.com 



Page 8 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 10 — Add some functions to the app delegate 



| I 4 ► HelloArduino - HelloArduino - m HelloArduinoViewtantroller.m ■ No Selection 



{ ! He 'Llo.ArdLiinoViewCantrall_er.nl 

// HelloArduino 

// 

// Created by Brian Jepson on 7/16/11. 

// Copyright 2B11 O'Reilly Media. All rights reserved. 

// 

#import "HelloArduinoViewController.h" 

^iiipleiientation HelloA rduin oViewC on t roller 
^synthesize toggleSwitch; 

- (uoiJ)dealloc 

[toggle5witch release] : 
[super dealloc] ; 

(voicSldidReceiveMernory Warning 

I! Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning] ; 

tl Release any cached data, images, etc that aren't in use. 
#pragma nark - View lifecycle 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 

- (void) viewDidLoad 

[super viewDidLoad] ; 

rscMgr - [[RscMgr alloc] init] ; 

[rscMgr setDelegate:__.elf] ; 



..voic I viewDioUnload 



[self setToggleSwitchinil] ; 
[super viewDidUnload] ; 



A 



• Select the 
HelloArduinoViewController.m file 
to open it for editing. 

• Visit my modified version of the file 
at GitHub then edit your copy of the 
file: 

• Locate the viewDidLoad 

method and replace it with my 
version of it. 

• Be sure to Remove the 
comment delimiters 
(/* and */) before and after 

it. 

• Implement the methods required 
by the RscMgrDelegate protocol 
(add everything from my file 

from the #pragma mark - 

RscMgrDelegate methods to 

the end of the file. 

• Locate the toggieLED method 
and replace it with my version of 
it. 

• Save the file (File->Save). 



© Make Projects 



www.makeprojects.com 



Page 9 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 11 — Declare support for the serial cable 







' -J 2 targets. i05 SDK 4.3 





Localization native development region 


String 


en 


J? ExiernalActessory. framework 
v [ ~~\ inc 
► □lib 
TCHHelloArduino 

[hj HelloArduirDAppDelegate.h 
|mj HelloArduinoAppDelegate.ni 
■-. MainWiridow.xib 
[hj HelloArduinoViewControlIer.il 
|m| HelloArduJnoViewController.m 
i$ HelloArduJnoViewControlIer.xib 
Supporting Files 





a 

a 




Bundle display name 


String 


^PRQDUCT_NAME} 


Executable file 


String 


HEXECuTTABLE_NAME| 


Icon file 


String 




Bundle identifier 


String 


com.makeprojecis.5[PRQDUCT_ 


InfoDictionary version 


String 


6.0 


Bundle name 


String 


51PRQDUCT_NAME1 


Bundle 05 Type code 


String 


APPL 


Bundle versions string, short 


String 


L.O 


Bundle creator OS Type code 


5tring 


7777 


Bundle version 


String 


L.O 






Application requires iPhone eiwironmei 


Boolean 


YES 


■ InfcPlist.strings 
[hj HelloArduin-o-Prefix. pen 
[m| m.ain.m 
HelloArduinoTests 
> Q~J Frameworks 
Products 


Main nib file base name 


String 


MainWindow + 


Supported interface orientations 


Array 


[3 items 1 


▼ Supported external accessory protocol* 


Array 


(1 item* 


Item 


String 


com. redpark. hobdo9 




^^^^^^^| 



A 



• This step is optional, but it 
will avoid the problem of you 
seeing an error message ("This 
accessory requires an 
application...") each time you plug 
in the cable. 

• In Xcode, expand the Supporting 
Files group and click on Hello- 
Arduino.plist to open it. 

• Right-click the bottom row, and 
choose Add Row. Click the 
up/down pointing arrows to the right 
of the new row's key (it will 
probably default to "Application 
Category") and choose "Supported 
external accessory protocols". 

• Click the triangle to the left of the 
key name you just selected to open 
up the list. In the value field for 
Item 0, type 

com. redpark . hobdb9. 

• Save the file (File^Save). 



© Make Projects 



www.makeprojects.com 



Page 10 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 12 — Deploy and test the Arduino sketch 



ff Arduino File Edit Sketch Tools Help 



RedparkToggle I Arduino 0022 




void setap() { 
Serial.! (9633); Start the serial pert 

pinMode(13, OUTPUT); // Set the bailt-iri LED to edtpat meaf 

]- 

void loop() { 

if (Serial. available()) { // If there's anything or the ser 
byte cmd - Serial .read(); // read a single byte from it. 

if (cmd -- '1') { // If the command is the character '1', 
diaitalVrit.e(13, HIGH); // set the LED or 

} 

if (cmd -- '0') { // If the command is the character '0', 
digitdlWrite(13, LOW); // set the LED off 
Serial. print(cmd, BYTE); 

} 




• Grab a copy of the sketch from 
GitHub . 

• Connect your Arduino to your 
computer. 

• Run the Arduino IDE and upload 
the RedparkToggle sketch to your 
Arduino. 

• Temporarily disconnect the jumper 
wire going to the Arduino's RX pin 
(the green cable shown in the first 
step), and upload your sketch. 

• You must disconnect it 
before uploading a sketch 
because the RS232-TTL adapter 
will otherwise interfere with the 
upload process. Leave the jumper 
wire disconnected until you 
complete the next step. 



A 



© Make Projects 



www.makeprojects.com 



Page 11 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 13 — Test the Arduino 





/dev/tty.usbserial-A7Q0dX7K 










l| i Send i 






SAutoscroll No line ending C ] 9600 baud ■*] 



• Open the Arduino's serial monitor 
(Tools->Serial Monitor), and make 
sure it's set to 9600 baud. 

• Wait a few seconds for the sketch 
to restart, then type 1 and click 
Send. The LED should come on. 
Try that again with instead of 1 , 
and the LED should go off. 

• Reconnect the jumper wire when 
you're done. 



Step 14 — Deploy the app 




• Connect your i Phone, iPad, or iPod 
touch to your Mac with a dock 
cable. 

• Make sure Xcode is configured to 
run it on the device you just 
connected (see the figure). 

• Click the Run button in Xcode. 

• If you get any errors, review 
the preceding steps to make 
sure you followed them exactly. If 
you still have problems, you can 
try downloading the project from 
GitHub. 



A 



© Make Projects 



www.makeprojects.com 



Page 12 of 13 



Connect an iPhone, iPad, or iPod touch to Arduino with the Redpark Serial Cable 



Step 15 — Run the app 



n 


^ 


1 . t , ;■ r . 






^S 


J ^B V 


k( 


m 


ti) 





• The app is running on your iOS 
device, but you need to swap 
cables next, so click Stop in Xcode 
and disconnect the dock cable. 

• Plug the Redpark Serial Cable into 
your device and into the RS232 
serial adapter. 

• Locate the HelloArduino app on the 
home screen, and run it. 



If everything is working correctly, you'll be able to turn the Arduino's built-in LED on and off by 
tapping the switch button! 



This document was last generated on 2012-11-03 03:13:04 AM. 



© Make Projects 



www.makeprojects.com 



Page 13 of 13